#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Feiniu

import numpy as np
import pandas as pd
from data_preprocessing import ML_data_preproccessing
from CB_recommendation import Content_based_recommendation
from CF_recommendation import Collaborative_Filtering_recommendation
from MF_recommendation import Matrix_Factorization
from measure import measure_method


if __name__ == '__main__':

    ML_data_preproccessing.data_preprocess()
    ML_data_preproccessing.data_preprocess_test()

    # 导入数据
    # movies_feature = pd.read_csv('../data/Moivelens/ml-latest-small/movies_feature.csv', index_col=0)
    user_rating = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\user-rating.csv', index_col=0)
    user_rating_test = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\user-rating_test.csv', index_col=0)

    # train, test = ML_data_preproccessing.train_test_split(user_rating)
    ratings_index = user_rating.index
    ratings_col = user_rating.columns
    train = pd.DataFrame(user_rating,
                         columns=ratings_col,
                         index= ratings_index)
    test = pd.DataFrame(user_rating_test,
                        columns=ratings_col,
                        index= ratings_index)
    pad = pd.DataFrame(np.zeros([10000,10000]),
                       columns =ratings_col,
                       index= ratings_index)



    # 使用基于内容的推荐算法来估计评分
    # count = 0
    # total = float(train.shape[0])
    # for idx, user in train.iterrows():
    #     unrated_index = user[user == 0].index.values
    #     rates_lst = []
    #
    #     for item in unrated_index:
    #         rate_h = Content_based_recommendation.CB_recommend_estimate(user, movies_feature, int(item))
    #         rates_lst.append(rate_h)
    #
    #     train.loc[idx, unrated_index] = rates_lst
    #
    #     count += 1
    #     if count % 100 == 0:
    #         presentage = round((count / total) * 100)
    #         print 'Completed %d' % presentage + '%'
    #
    # train.to_csv('../data/Moivelens/ml-latest-small/pred_ratings_CB.csv')


    # 使用user-user的协同过滤算法来估计评分
    count = 0
    total = float(train.shape[0])
    for idx, user in test.iterrows():
        unrated_index = user[user > 0].index.values
        unrated_index_ = map(int, unrated_index)
        rates_lst = Collaborative_Filtering_recommendation.CF_recommend_estimate(train, idx, unrated_index_, 50)

        pad.loc[idx, unrated_index] = rates_lst

        count += 1
        if count % 100 == 0:
            presentage = round((count / total) * 100)
            print('uu Completed %d' % presentage + '%')

    pad.to_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\pred_ratings_CF.csv')

    # 计算协同过滤的RMSE
    # pred_CF = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\pred_ratings_CF.csv', index_col=0)
    # nonzero_index = user_rating.values.nonzero()
    # actual = user_rating.values[nonzero_index[0], nonzero_index[1]]
    # pred_CF = pred_CF.values[nonzero_index[0], nonzero_index[1]]
    pred_CF = pd.read_csv('C:\\Users\\lyf\\Desktop\\赵易聪\\大数据分析B\\HW2\\Project2-data\\pred_ratings_CF.csv', index_col=0)
    nonzero_index = user_rating.values.nonzero()
    actual = np.array(test)
    pred_CF = np.array(pred_CF)
    print('RMSE of CF is %s' % measure_method.comp_rmse(pred_CF, actual))


    # # 使用矩阵分解算法来估计评分
    # MF_estimate = Matrix_Factorization.Matrix_Factorization(K=50, epoch=20)
    # MF_estimate.fit(train)
    # R_hat = MF_estimate.start()
    # non_index = test.values.nonzero()
    # pred_MF = R_hat[non_index[0], non_index[1]]
    # actual = test.values[non_index[0], non_index[1]]

    # print('MSE of MF is %s' % measure_method.comp_mse(pred_MF, actual))
    # print('RMSE of MF is %s' % measure_method.comp_rmse(pred_MF, actual))
